package com.youdianda.common;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * 分页结果集
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageResult {

    //总记录数
    private Integer count;
    //总页数
    private Integer totalPages;
    //当前页数
    private Integer currentPage;
    //每页展示条数
    private Integer pageSize;
    //展示数据
    private Object data;


    /**
     * 获取不需要用到dto的分页数据
     * @param page
     * @return
     */
    public static PageResult getResult(Page page){

        PageResult pageResult = new PageResult();
        pageResult.setCount((int) page.getTotal());
        pageResult.setTotalPages((int) page.getPages());
        pageResult.setCurrentPage((int) page.getCurrent());
        pageResult.setPageSize((int) page.getSize());
        pageResult.setData(page.getRecords());

        return pageResult;

    }

    /**
     * 获取需要用到dto的分页数据
     * @param page
     * @param records
     * @return
     */
    public static PageResult getResultByDtoPage(Page page , List records){

        PageResult pageResult = new PageResult();
        pageResult.setCount((int) page.getTotal());
        pageResult.setTotalPages((int) page.getPages());
        pageResult.setCurrentPage((int) page.getCurrent());
        pageResult.setPageSize((int) page.getSize());
        pageResult.setData(records);

        return pageResult;

    }

}